﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
    public class KhachHangDAO
    {
        // 0712455
        public static List<KhachHangDTO> TimKhachHang(KhachHangDTO khDto)
        {
            SqlConnection cnn = KetNoiCSDL.TaoKetNoi();

            string query = "select * from Khach_Hang where ";

            if (khDto.HoTen != null)
            {
                query = query + "HoTen = @HoTen and ";
            }
            if (khDto.CMND != null)
            {
                query = query + "CMND = @CMNDkh and ";
            }
            query = query + "(1 = 1)";

            SqlCommand cmd = new SqlCommand(query, cnn);

            if (khDto.HoTen != null)
            {
                cmd.Parameters.Add("@HoTen", SqlDbType.NVarChar);
                cmd.Parameters["@HoTen"].Value = khDto.HoTen;
            }
            if (khDto.CMND != null)
            {
                cmd.Parameters.Add("@CMNDkh", SqlDbType.VarChar);
                cmd.Parameters["@CMNDkh"].Value = khDto.CMND;
            }

            cnn.Open();
            List<KhachHangDTO> lstKH = new List<KhachHangDTO>();
            try
            {
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    KhachHangDTO khachHangDto = new KhachHangDTO();

                    khachHangDto.MaKH = (int)dr["MaKH"];
                    khachHangDto.HoTen = dr["HoTen"].ToString();
                    khachHangDto.CMND = dr["CMND"].ToString();
                    khachHangDto.DiaChi = dr["DiaChi"].ToString();

                    lstKH.Add(khachHangDto);
                }
                cnn.Close();
                return lstKH;
            }
            catch (Exception ex)
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw ex;
            }
            
        }

        // 0712455 begin
        private static int LayMaKH()
        {
            SqlConnection cnn = KetNoiCSDL.TaoKetNoi();
            string query = "select count(*) from Khach_Hang";
            SqlCommand cmd = new SqlCommand(query, cnn);

            cnn.Open();
            try
            {
                return (int)cmd.ExecuteScalar() + 1;
            }
            catch (Exception ex)
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw ex;
            }
            finally
            {
                cnn.Dispose();
            }
        }

        public static int Them(KhachHangDTO khDto)
        {
            khDto.MaKH = LayMaKH();

            khDto.SoDT = "2323";

            SqlConnection cnn = KetNoiCSDL.TaoKetNoi();
            string query = "insert into Khach_Hang values (@MaKH,@HoTen,@CMND,@NgayCap,@NoiCap,@DiaChi,@SoDT)";
            SqlCommand cmd = new SqlCommand(query, cnn);

            cmd.Parameters.Add("@MaKH", SqlDbType.Int);
            cmd.Parameters.Add("@HoTen", SqlDbType.NVarChar);
            cmd.Parameters.Add("@CMND", SqlDbType.VarChar);
            cmd.Parameters.Add("@NgayCap", SqlDbType.DateTime);
            cmd.Parameters.Add("@NoiCap", SqlDbType.NVarChar);
            cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar);
            cmd.Parameters.Add("@SoDT", SqlDbType.VarChar);

            cmd.Parameters["@MaKH"].Value = khDto.MaKH;
            cmd.Parameters["@HoTen"].Value = khDto.HoTen;
            cmd.Parameters["@CMND"].Value = khDto.CMND;
            cmd.Parameters["@NgayCap"].Value = khDto.NgayCapCMND;
            cmd.Parameters["@NoiCap"].Value = khDto.NoiCapCMND;
            cmd.Parameters["@DiaChi"].Value = khDto.DiaChi;
            cmd.Parameters["@SoDT"].Value = khDto.SoDT;

            cnn.Open();
            try
            {
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw ex;
            }
            finally
            {
                cnn.Dispose();
            }

        }
        
       
        public static KhachHangDTO LayKhachHang(int MaKH)
        {
            SqlConnection cnn = KetNoiCSDL.TaoKetNoi();
            string query = String.Format("select * from Khach_Hang where MaKH = {0}", MaKH);
            SqlCommand cmd = new SqlCommand(query, cnn);

            cnn.Open();

            try
            {
                SqlDataReader dr = cmd.ExecuteReader();
                KhachHangDTO khDto = new KhachHangDTO();
                while (dr.Read())
                {
                    khDto.MaKH = (int)dr["MaKH"];
                    khDto.HoTen = dr["HoTen"].ToString();
                    khDto.CMND = dr["CMND"].ToString();
                    khDto.DiaChi = dr["DiaChi"].ToString();
                }
                cnn.Close();
                return khDto;
            }
            catch (Exception ex)
            {
                cnn.Close();
                throw ex;
            }
        }
        // end 0712455
    }
}

